package cn.cs.controller;

import cn.cs.client.SysDtAreaClient;
import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmailUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.pojo.AgentInvoice;
import cn.cs.service.AgentLevelbPayService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value = "/agentLevelbPay")
public class AgentLevelbPayController {

    @Resource
    private AgentLevelbPayService agentLevelbPayService;

    @Resource
    private SysDtAreaClient sysDtAreaClient;


    /**
     * 分页查询 B级经销商支付记录
     * @param pageNumber
     * @param pageSize
     * @param agentId
     * @param person 负责人
     * @param phone 手机号
     * @param provinceId 省份id
     * @param cityId 市id
     * @param areaId 区域id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getAgentLevelbPayByPage")
    public Dto getAgentLevelbPayByPage(Integer pageNumber, Integer pageSize, String agentId, String person, String phone, String provinceId, String cityId, String areaId) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("agentId", agentId);
        param.put("person", person);
        param.put("phone", phone);
//        param.put("provinceId", provinceId);
//        param.put("cityId", cityId);
//        param.put("areaId", areaId);
        if(EmptyUtils.isNotEmpty(provinceId)){
            param.put("list", sysDtAreaClient.getAreaListByProvinceId(provinceId));
        }
        if(EmptyUtils.isNotEmpty(cityId)){
            param.put("list", sysDtAreaClient.getAreaListByCityId(cityId));
        }
        if(EmptyUtils.isNotEmpty(areaId)){
            param.put("list", sysDtAreaClient.getAreaListById(areaId));
        }
        return DtoUtil.returnSuccess("获取成功", agentLevelbPayService.queryAgentLevelbPayPageByMap(param, pageNumber, pageSize));
    }

    /**
     * 上传发票图片
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/uploadInvoiceImg")
    public Dto uploadInvoiceImg(HttpServletRequest request) throws Exception{
        String id = request.getParameter("invoiceId");//发票Id
        String einvoice_img_url = request.getParameter("einvoice_img_url");//发票图片Url
        AgentInvoice agentInvoice = new AgentInvoice();
        agentInvoice.setId(id);
        agentInvoice.setEinvoiceImgUrl(einvoice_img_url);
        return DtoUtil.returnSuccess("上传成功", agentLevelbPayService.modifyAgentInvoice(agentInvoice));
    }

    /**
     * 发票申请发送邮件
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/sendInvoiceEmail")
    public Dto sendInvoiceEmail(String title, String content, String img, String email) throws Exception{
        EmailUtil.sendEmail(email, title, content, img);//发送邮件
        return DtoUtil.returnSuccess("邮件发送成功", "");
    }


}
